/*
@param name name of the ring
@param radius size of the ring
@param color color of the ring
@param color2 color of the marker
@param color3 color of the pointer
@param division division per a full circle
@param font font of the marker
@param canvasId
*/
var days= new Array();
days[0]="Sun";
days[1]="Mon";
days[2]="Tus";
days[3]="Wed";
days[4]="Thu";
days[5]="Fri";
days[6]="Sat";

var months= new Array();
months[0]="Jan";
months[1]="Feb";
months[2]="Mar";
months[3]="Apr";
months[4]="May";
months[5]="Jun";
months[6]="Jul";
months[7]="Aug";
months[8]="Sep";
months[9]="Oct";
months[10]="Nov";
months[11]="Dec";



function Ring (name,radius, color, color2, color3, division, font, canvasId)
{
	this.name=name;
	this.radius=radius;
	this.color=color;
	this.color2=color2;
	this.color3=color3;
	this.division=division;
	this.canvasId=canvasId;
	this.font=font;
	//get context from canvas ID
	this.canvas  = document.getElementById(canvasId);
    this.ctx = this.canvas.getContext('2d');
        
	//find centre of the canvas	
    this.centreX = this.canvas.width / 2;
    this.centreY = (this.canvas.height / 2);
}

//draw the fixed hand, only call it once
Ring.prototype.drawHand=function()
{
    //draw Line
	this.ctx.beginPath();
    this.ctx.moveTo(this.centreX, 0);
	this.ctx.lineTo(this.centreX, this.centreY);
	this.ctx.strokeStyle= "#000000";
	this.ctx.stroke();
	this.ctx.closePath();
}

//only to show moon phase
Ring.prototype.drawMoonPhase=function()
{
    if(this.name=="moon")
	{	
		var moonphase=moonPhase();
		if(moonphase>=0)
		{
		    var image= new Image();
			image.align="middle";
			if(moonphase==0)
			{
			    image.src="moonphases/new.jpg";
			}
			else if(moonphase==1)
			{
			    image.src="moonphases/waxing_crescent.jpg";
			}
			else if(moonphase==2)
			{
			    image.src="moonphases/first_quarter.jpg";
			}
			else if(moonphase==3)
			{
			    image.src="moonphases/waxing_gibbous.jpg";
			}
			else if(moonphase==4)
			{
			    image.src="moonphases/full.jpg";
			}
			else if(moonphase==5)
			{
			    image.src="moonphases/waning_gibbous.jpg";
			}
			else if(moonphase==6)
			{
			    image.src="moonphases/last_quarter.jpg";
			}
			else if(moonphase==7)
			{
			    image.src="moonphases/waning_crescent.jpg";
			}
			
		}
		this.ctx.drawImage(image, this.centreX-30, this.centreY-30);
	}
}

Ring.prototype.drawRing=function()
{
    this.ctx.fillStyle = this.color;
	this.ctx.strokeStyle= this.color;
	this.ctx.beginPath();
	this.ctx.arc(this.centreX,this.centreY,this.radius,0,Math.PI*2,true);
	this.ctx.closePath();
	this.ctx.fill();	
}


Ring.prototype.addMarker=function()
{
    var now=new Date();
	var sec = now.getSeconds();
    var min = now.getMinutes();
    var hr  = now.getHours();
	var day = now.getDay();
	var date = now.getDate();
	var month = now.getMonth();
	var year = now.getFullYear();
	
	var position;
	if(this.name=='secs')
	{
	    position=sec;
	}
	else if(this.name=='mins')
	{
	    position=min;
	}
	else if(this.name=='hrs')
	{
	    position=hr;
	}
	else if(this.name=='week')
	{
	    position=day;
	}
	else if(this.name=='date')
	{
	    position=date;
	}
	else if(this.name=='month')
	{
	    position=month;
	}
	this.ctx.font=this.font;
	//this.ctx.fillStyle=this.color2;
	this.ctx.textAlign="center";
	this.ctx.textBaseline="middle";

	var startAngle=-Math.PI/2;
	var changeAngle=2*Math.PI/(this.division);
	if (this.name=="date")
	{
		//date is non periodic
	    //so the clock is going to show 30 days
		//into the future
		this.ctx.fillStyle=this.color2;
		for(i=0; i<30; i++)
		{
			var x=Math.cos(startAngle);
		    var y=Math.sin(startAngle);
		
		    x=(this.radius-10)*x;
		    y=(this.radius-10)*y;
		
		    x=x+this.centreX;
		    y=y+this.centreY;
			
		    var future = new Date();
			if(i!=0)
			{
			    future.setDate(future.getDate()+i);
			}
		    this.ctx.fillText(future.getDate(), x, y);
			for(var j=0; j<monthlyEvents.length; j++)
			{
				var temp=monthlyEvents[j][0].split("/");
				var img=monthlyEvents[j][1];
				var des=monthlyEvents[j][2];
				//javascrpit bug
				var temp2=parseInt(temp[1],10);
				var image= new Image();
				image.src=img;
				image.align="middle";
                
				
				if(temp2!=date && temp2==future.getDate())
				{
					this.ctx.drawImage(image, x-5, y, 25, 25);
				}
				
			}
			for(var j=0; j<yearlyEvents.length; j++)
			{
				var temp=yearlyEvents[j][0].split("/");
				var img=yearlyEvents[j][1];
				var des=yearlyEvents[j][2];
				//javascrpit bug
				//month in cookie
				var temp2=parseInt(temp[0],10)-1;
				//date in cookie
				var temp3=parseInt(temp[1]);
				var image= new Image();
				image.src=img;
				image.align="middle";

				if(temp2==month && temp3==future.getDate())
				{
					this.ctx.drawImage(image, x-5, y, 25, 25);
				}
			}
			
			for(var j=0; j<oneOffEvents.length; j++)
			{
				var tempDate=oneOffEvents[j][0].split("/");
				
				var tempTime=oneOffEvents[j][1].split(":");
				var img=oneOffEvents[j][2];
				var des=oneOffEvents[j][3];
				//javascrpit bug
				//month in cookie

				var tempMonth=parseInt(tempDate[0],10)-1;
				//date in cookie
				var tempDay=parseInt(tempDate[1]);
				var tempYear=parseInt(tempDate[2]);
				var tempHour=parseInt(tempTime[0],10);
				var tempMin=parseInt(tempTime[1],10);

				var image= new Image();
				image.src=img;
				image.align="middle";

				if(tempYear==year && tempMonth==month && tempDay>date && tempDay==future.getDate())
				{
					this.ctx.drawImage(image, x-5, y, 25, 25);
				}

			}

			
			
			startAngle=startAngle+changeAngle;
			
		}
	}
	else
	{
        for (var i=position;i<this.division;i++)
	    {
	        if(i==position)
		    {
		        this.ctx.fillStyle=this.color3;
		    }
		    else
		    {
		        this.ctx.fillStyle=this.color2;
		    }
	        var x=Math.cos(startAngle);
		    var y=Math.sin(startAngle);
		
		    x=(this.radius-10)*x;
		    y=(this.radius-10)*y;
		
		    x=x+this.centreX;
		    y=y+this.centreY;
		    if (this.name=="week")
		    {
		        this.ctx.fillText(days[i], x, y);
		    }
		    else if(this.name=="month")
		    {
		        this.ctx.fillText(months[i], x, y);
				for(var j=0; j<yearlyEvents.length; j++)
				{
					var temp=yearlyEvents[j][0].split("/");
					var img=yearlyEvents[j][1];
					var des=yearlyEvents[j][2];
					//javascrpit bug
					//month in cookie
					var temp2=parseInt(temp[0],10)-1;
					//date in cookie
					var temp3=parseInt(temp[1]);
					var image= new Image();
					image.src=img;
					image.align="middle";

					if(temp2!=month && temp2==i)
					{
						this.ctx.drawImage(image, x-5, y, 25, 25);
					}
				}
				
			for(var j=0; j<oneOffEvents.length; j++)
			{
				var tempDate=oneOffEvents[j][0].split("/");
				
				var tempTime=oneOffEvents[j][1].split(":");
				var img=oneOffEvents[j][2];
				var des=oneOffEvents[j][3];
				//javascrpit bug
				//month in cookie

				var tempMonth=parseInt(tempDate[0],10)-1;
				//date in cookie
				var tempDay=parseInt(tempDate[1]);
				var tempYear=parseInt(tempDate[2]);
				var tempHour=parseInt(tempTime[0],10);
				var tempMin=parseInt(tempTime[1],10);

				var image= new Image();
				image.src=img;
				image.align="middle";

				if(tempYear==year && tempMonth>month && tempMonth==i)
				{
					this.ctx.drawImage(image, x-5, y, 25, 25);
				}
				//next year's one off event still need work
				else if(tempYear==(year+1) && tempMonth<month && tempMonth==i)
				{
				    this.ctx.drawImage(image, x-5, y, 25, 25);
				}
				

			}
		    }
		    else
		    {
		        if(this.name=="secs")
			    {
		           if(i%5==0)
			       {
		                this.ctx.fillText(i, x, y);
			       }
			       else
			       {
	                this.ctx.beginPath();
	                this.ctx.arc(x,y,2.3,0,Math.PI*2,true);
	                this.ctx.closePath();
	                this.ctx.fill();
			       }
			    }
			    else
			    {
			        this.ctx.fillText(i, x, y);
					if(this.name=="hrs")
					{
					    for(var j=0; j<dailyEvents.length; j++)
						{
						    var time=dailyEvents[j][0].split(":");
							var img=dailyEvents[j][1];
							var des=dailyEvents[j][2];
							//javascrpit bug
							var hour=parseInt(time[0],10);
							var minute=parseInt(time[1]);
							var image= new Image();
							image.src=img;
							image.align="middle";

							if(hour!=hr && hour==i)
							{
							    this.ctx.drawImage(image, x-5, y, 25, 25);
							}
						}
						
						
                     	for(var j=0; j<oneOffEvents.length; j++)
			            {
				            var tempDate=oneOffEvents[j][0].split("/");
			
				            var tempTime=oneOffEvents[j][1].split(":");
				            var img=oneOffEvents[j][2];
				            var des=oneOffEvents[j][3];
				            //javascrpit bug
				            //month in cookie

				            var tempMonth=parseInt(tempDate[0],10)-1;
				            //date in cookie
				            var tempDay=parseInt(tempDate[1]);
				            var tempYear=parseInt(tempDate[2]);
				            var tempHour=parseInt(tempTime[0],10);
				            var tempMin=parseInt(tempTime[1],10);

				            var image= new Image();
				            image.src=img;
				            image.align="middle";

				            if(tempYear==year && tempMonth==month && tempDay==date && tempHour>hr && tempHour==i)
				            {
					            this.ctx.drawImage(image, x-5, y, 25, 25);
				            }

			             }
						 
					}
					else if(this.name=="mins")
					{
					    for(var j=0; j<dailyEvents.length; j++)
						{
						    var time=dailyEvents[j][0].split(":");
							var img=dailyEvents[j][1];
							var des=dailyEvents[j][2];
							//javascrpit bug
							var hour=parseInt(time[0],10);
							var minute=parseInt(time[1]);
							var image= new Image();
							image.src=img;
							image.align="middle";

							if(hour==hr && minute==i && minute>=min)
							{
							    this.ctx.drawImage(image, x-5, y, 25, 25);
							}
						}
						
						for(var j=0; j<oneOffEvents.length; j++)
			            {
				            var tempDate=oneOffEvents[j][0].split("/");
			
				            var tempTime=oneOffEvents[j][1].split(":");
				            var img=oneOffEvents[j][2];
				            var des=oneOffEvents[j][3];
				            //javascrpit bug
				            //month in cookie

				            var tempMonth=parseInt(tempDate[0],10)-1;
				            //date in cookie
				            var tempDay=parseInt(tempDate[1]);
				            var tempYear=parseInt(tempDate[2]);
				            var tempHour=parseInt(tempTime[0],10);
				            var tempMin=parseInt(tempTime[1],10);

				            var image= new Image();
				            image.src=img;
				            image.align="middle";

				            if(tempYear==year && tempMonth==month && tempDay==date && tempHour==hr && tempMin>min && tempMin==i)
				            {
					            this.ctx.drawImage(image, x-5, y, 25, 25);
				            }

			             }
					}
			    }
		    }
		
		    startAngle=startAngle+changeAngle;
		    this.ctx.fillStyle=this.color2;
        }
	
	    for (var i=0;i<position;i++)
	    {
	        var x=Math.cos(startAngle);
		    var y=Math.sin(startAngle);
		
		    x=(this.radius-10)*x;
		    y=(this.radius-10)*y;
		
		    x=x+this.centreX;
		    y=y+this.centreY;
		    if (this.name=="week")
		    {
		        this.ctx.fillText(days[i], x, y);
		    }
		    else if(this.name=="month")
		    {
		        this.ctx.fillText(months[i], x, y);
				for(var j=0; j<yearlyEvents.length; j++)
				{
					var temp=yearlyEvents[j][0].split("/");
					var img=yearlyEvents[j][1];
					var des=yearlyEvents[j][2];
					//javascrpit bug
					//month in cookie
					var temp2=parseInt(temp[0],10)-1;
					//date in cookie
					var temp3=parseInt(temp[1]);
					var image= new Image();
					image.src=img;
					image.align="middle";

					if(temp2!=month && temp2==i)
					{
						this.ctx.drawImage(image, x-5, y, 25, 25);
					}
				}
				
			for(var j=0; j<oneOffEvents.length; j++)
			{
				var tempDate=oneOffEvents[j][0].split("/");
				
				var tempTime=oneOffEvents[j][1].split(":");
				var img=oneOffEvents[j][2];
				var des=oneOffEvents[j][3];
				//javascrpit bug
				//month in cookie

				var tempMonth=parseInt(tempDate[0],10)-1;
				//date in cookie
				var tempDay=parseInt(tempDate[1]);
				var tempYear=parseInt(tempDate[2]);
				var tempHour=parseInt(tempTime[0],10);
				var tempMin=parseInt(tempTime[1],10);

				var image= new Image();
				image.src=img;
				image.align="middle";

				if(tempYear==year && tempMonth>month && tempMonth==i)
				{
					this.ctx.drawImage(image, x-5, y, 25, 25);
				}
				//next year's one off event still need work
				else if(tempYear==(year+1) && tempMonth<month && tempMonth==i)
				{
				    this.ctx.drawImage(image, x-5, y, 25, 25);
				}
				

			}
				
				

		    }
		    else
		    {
		        if(this.name=="secs")
			    {
		           if(i%5==0)
			       {
		               this.ctx.fillText(i, x, y);
			       }
			       else
			       {
			   	    this.ctx.fillStyle=this.color2;
	                this.ctx.beginPath();
	                this.ctx.arc(x,y,2.3,0,Math.PI*2,true);
	                this.ctx.closePath();
	                this.ctx.fill();
			        }
			    }
			    else
			    {
			        this.ctx.fillText(i, x, y);
								 
					if(this.name=="hrs")
					{
					    for(var j=0; j<dailyEvents.length; j++)
						{
						    var time=dailyEvents[j][0].split(":");
							var img=dailyEvents[j][1];
							var des=dailyEvents[j][2];
							//javascrpit bug
							var hour=parseInt(time[0],10);
							var minute=parseInt(time[1]);
							var image= new Image();
							image.src=img;
							image.align="middle";

							if(hour!=hr && hour==i)
							{
							    this.ctx.drawImage(image, x-5, y, 25, 25);
							}
						}
						
						for(var j=0; j<oneOffEvents.length; j++)
			            {
				            var tempDate=oneOffEvents[j][0].split("/");
				
				            var tempTime=oneOffEvents[j][1].split(":");
				            var img=oneOffEvents[j][2];
				            var des=oneOffEvents[j][3];
				            //javascrpit bug
				            //month in cookie

				            var tempMonth=parseInt(tempDate[0],10)-1;
				            //date in cookie
				            var tempDay=parseInt(tempDate[1]);
				            var tempYear=parseInt(tempDate[2]);
				            var tempHour=parseInt(tempTime[0],10);
				            var tempMin=parseInt(tempTime[1],10);

				            var image= new Image();
				            image.src=img;
				            image.align="middle";

				            if(tempYear==year && tempMonth==month && tempDay==date && tempHour>hr && tempHour==i)
				            {
					            this.ctx.drawImage(image, x-5, y, 25, 25);
				            }

			             }
						 
					}
					
					else if(this.name=="mins")
					{
					    for(var j=0; j<dailyEvents.length; j++)
						{
						    var time=dailyEvents[j][0].split(":");
							var img=dailyEvents[j][1];
							var des=dailyEvents[j][2];
							//javascrpit bug
							var hour=parseInt(time[0],10);
							var minute=parseInt(time[1]);
							var image= new Image();
							image.src=img;
							image.align="middle";

							if(hour==hr && minute==i && minute>=min)
							{
							    this.ctx.drawImage(image, x-5, y, 25, 25);
							}
						}
						
						for(var j=0; j<oneOffEvents.length; j++)
			            {
				            var tempDate=oneOffEvents[j][0].split("/");
			
				            var tempTime=oneOffEvents[j][1].split(":");
				            var img=oneOffEvents[j][2];
				            var des=oneOffEvents[j][3];
				            //javascrpit bug
				            //month in cookie

				            var tempMonth=parseInt(tempDate[0],10)-1;
				            //date in cookie
				            var tempDay=parseInt(tempDate[1]);
				            var tempYear=parseInt(tempDate[2]);
				            var tempHour=parseInt(tempTime[0],10);
				            var tempMin=parseInt(tempTime[1],10);

				            var image= new Image();
				            image.src=img;
				            image.align="middle";

				            if(tempYear==year && tempMonth==month && tempDay==date && tempHour==hr && tempMin>min && tempMin==i)
				            {
					            this.ctx.drawImage(image, x-5, y, 25, 25);
				            }

			             }
					}
			    }
		    }
		
		    startAngle=startAngle+changeAngle;
        }
	}
	
}

/*
Ring.prototype.addPointer=function()
{
    var now=new Date();
	var sec = now.getSeconds();
    var min = now.getMinutes();
    var hr  = now.getHours();
	var day = now.getDay();
	var date = now.getDate();
	var month = now.getMonth();
	var angle;
	var x;
	var y;
	
	//draw second hand
	if(this.name=="secs")
	{
	    angle=-Math.PI/2+(2*Math.PI/(this.division))*sec;
	}
	else if(this.name=="mins")
	{
	    angle=-Math.PI/2+(2*Math.PI/(this.division))*min;
	}
	else if(this.name=="hrs")
	{
	    angle=-Math.PI/2+(2*Math.PI/(this.division))*hr;
	}
	else if(this.name=="week")
	{
	    angle=-Math.PI/2+(2*Math.PI/(this.division))*day;
	}
    else if(this.name=="date")
	{
	    angle=-Math.PI/2+(2*Math.PI/(this.division))*(date-1);// date starts with 1 not 0;
	}
	else if(this.name=="month")
	{
	    angle=-Math.PI/2+(2*Math.PI/(this.division))*month;
	}
	x=Math.cos(angle);
    y=Math.sin(angle);
		
    x=(this.radius-10)*x;
    y=(this.radius-10)*y;
		
    x=x+this.centreX;
    y=y+this.centreY;
	this.ctx.fillStyle=this.color3;
	this.ctx.globalAlpha=0.5
	this.ctx.beginPath();
	this.ctx.arc(x,y,10,0,Math.PI*2,true);
	this.ctx.closePath();
	this.ctx.fill();
	
	//resore transparency to full
	this.ctx.globalAlpha=1.0;
}
*/